home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-08-25 | 5.3 KB | 276 lines | [TEXT/MPS ] |
- /*
- File: DocWindow.cp
-
- Contains: A simple document window
-
- Written by: Dave Falkenburg
-
- Copyright: © 1993-94 by Dave Falkenburg, all rights reserved.
-
- Change History (most recent first):
-
-
- To Do: Figure out if any of these methods should move to TWindow
-
- */
-
- #include "DocWindow.h"
- #include "AppLib.h"
- #include <LowMem.h> // for LMGetCurApName()
- #include <ToolUtils.h> // for NumToString()
- #include <Icons.h>
- #include <Threads.h>
-
- unsigned long TDocWindow::fgUntitledTagCount = 0;
-
-
- const short kHeaderHeight = 20;
- const short kScrollbarWidth = 15;
-
- const short kFirstSpinningArrowIconID = 801;
- const short kLastSpinningArrowIconID = 808;
-
- const short kDocWindowTemplateID = 1026;
-
-
- static void
- SpinArrowsThreadProc(TDocWindow * windowToSpin)
- {
- while (1)
- {
- windowToSpin->SpinHeaderArrows();
- YieldToAnyThread();
- }
- }
-
-
- static void
- MyDrawGrowIcon(WindowPtr aWindow)
- {
- Rect r = aWindow->portRect;
- RgnHandle savedClip = NewRgn();
-
- GetClip(savedClip);
-
- r.top += kHeaderHeight;
- ClipRect(&r);
- DrawGrowIcon(aWindow);
- SetClip(savedClip);
- DisposeRgn(savedClip);
- }
-
-
- TDocWindow::TDocWindow()
- {
- OSErr err;
-
- fCurrentSpinningArrowIconID = kFirstSpinningArrowIconID;
-
- TDocWindow::fgUntitledTagCount++; // Starts out as zero but we want “Untitled-1”
- this->CreateWindow();
-
- if (gHasThreadManager)
- {
- err = NewThread(kCooperativeThread,(ThreadEntryProcPtr) SpinArrowsThreadProc,this,0,0,nil,&fSpinnerThreadID);
- if (err != noErr)
- DebugStr((StringPtr) "\pNewThread failed");
- }
- }
-
- TDocWindow::~TDocWindow()
- {
- OSErr err;
-
- // NEWS OF THE WEIRD!
- // If you pass true for recyleThread param, the heap block for the stack isn’t
- // disposed— it gets placed in the default cooperative thread pool of “premade”
- // threads.
- //
- // To optimize memory usage, I might just want to go ahead and create
- // a thread pool at application startup so we can avoid moving memory around
- // so much.
-
- if (gHasThreadManager)
- {
- err = DisposeThread(fSpinnerThreadID,nil,false);
- if (err != noErr)
- DebugStr((StringPtr) "\pDisposeThread failed");
- }
- }
-
-
- WindowPtr
- TDocWindow::MakeNewWindow(WindowPtr behindWindow)
- {
- WindowPtr aWindow = GetNewColorOrBlackAndWhiteWindow(kDocWindowTemplateID,nil,behindWindow);
- Str255 titleString;
-
- if (aWindow)
- {
- // make the window exciting & different
-
- // Can’t call nudge because it assumes the window is visible!
-
- // Nudge(fgUntitledTagCount * kHeaderHeight,fgUntitledTagCount * kHeaderHeight);
-
- GetWTitle(aWindow,titleString);
- if (StrLength(titleString) != 0)
- {
- Str255 numberString;
-
- NumToString(fgUntitledTagCount,numberString);
- BlockMove(&numberString[1],&titleString[titleString[0]+1],numberString[0]);
- titleString[0] += numberString[0];
- }
-
- SetWTitle(aWindow,titleString);
- ShowWindow(aWindow);
- }
-
- return aWindow;
- }
-
-
- void
- TDocWindow::Activate(Boolean /* activating */)
- {
- MyDrawGrowIcon(fWindow);
- }
-
-
- void
- TDocWindow::AdjustCursor(EventRecord * /* anEvent */)
- {
- }
-
-
- void
- TDocWindow::Draw(void)
- {
- EraseRect(&fWindow->portRect);
-
- MoveTo(0,kHeaderHeight-3); LineTo(fWindow->portRect.right,kHeaderHeight-3);
- MoveTo(0,kHeaderHeight-1); LineTo(fWindow->portRect.right,kHeaderHeight-1);
-
- MyDrawGrowIcon(fWindow);
- }
-
-
- void
- TDocWindow::Click(EventRecord * /* anEvent */)
- {
- this->Select();
- }
-
-
- void
- TDocWindow::AdjustForNewWindowSize(Rect *oldSize,Rect * /* newSize */)
- {
- Rect scrollbarRect;
-
- // Invalidate the old vertical scroll bar
-
- scrollbarRect.top = oldSize->top;
- scrollbarRect.left = oldSize->right - kScrollbarWidth;
- scrollbarRect.bottom = oldSize->bottom;
- scrollbarRect.right = oldSize->right;
- InvalRect(&scrollbarRect);
-
- // Invalidate the old horizontal scroll bar
-
- scrollbarRect.left = oldSize->left;
- scrollbarRect.top = oldSize->bottom - kScrollbarWidth;
- InvalRect(&scrollbarRect);
-
- MyDrawGrowIcon(fWindow);
- }
-
-
- Boolean
- TDocWindow::Close(void)
- {
- StandardCloseResult result;
- Str255 title;
-
- GetWTitle(this->fWindow,title);
- result = StandardCloseDocument(LMGetCurApName(),title,false,false);
-
- if (result != kCancelSaveDocument)
- {
- return TWindow::Close();
- }
- return false;
- }
-
-
- OSErr
- TDocWindow::DragEnterWindow(DragReference theDrag)
- {
- return this->DragInWindow(theDrag);
- }
-
-
- OSErr
- TDocWindow::DragInWindow(DragReference theDrag)
- {
- RgnHandle hiliteRgn = NewRgn();
- Point mouseLoc, pinnedMouseLoc;
-
- SetRectRgn( hiliteRgn,
- fWindow->portRect.left,
- fWindow->portRect.top + kHeaderHeight,
- fWindow->portRect.right-kScrollbarWidth,
- fWindow->portRect.bottom-kScrollbarWidth);
-
- (void) GetDragMouse(theDrag,&mouseLoc,&pinnedMouseLoc);
- GlobalToLocal(&mouseLoc);
-
- if (PtInRgn(mouseLoc,hiliteRgn))
- ShowDragHilite(theDrag,hiliteRgn,true);
- else
- HideDragHilite(theDrag);
-
- DisposeRgn(hiliteRgn);
- return noErr;
- }
-
-
- OSErr
- TDocWindow::DragLeaveWindow(DragReference theDrag)
- {
- HideDragHilite(theDrag);
- return noErr;
- }
-
-
- OSErr
- TDocWindow::HandleDrop(DragReference /* theDrag */)
- {
- return noErr;
- }
-
-
- void
- TDocWindow::SpinHeaderArrows()
- {
- GrafPtr oldPort;
-
- GetPort(&oldPort);
- SetPort(fWindow);
-
- Rect r;
-
- fCurrentSpinningArrowIconID++;
-
- if (fCurrentSpinningArrowIconID > kLastSpinningArrowIconID)
- fCurrentSpinningArrowIconID = kFirstSpinningArrowIconID;
-
- r.top = fWindow->portRect.top;
- r.left = fWindow->portRect.left+4;
- r.bottom = r.top + 16;
- r.right = r.left + 16;
- (void) PlotIconID(&r,atNone,ttNone,fCurrentSpinningArrowIconID);
-
- SetPort(oldPort);
- }
-